﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace ShohdiWindowsVersion
{
    public class WindowsVersionClass
    {
        public static double getWindowsVer()
        {
            try
            {
                string strAssemply = System.Reflection.Assembly.GetExecutingAssembly().Location;
                FileInfo fiCurrent = new FileInfo(strAssemply);
                string strDir = fiCurrent.DirectoryName;
                if (!strDir.EndsWith("\\"))
                {
                    strDir += "\\";
                }

                string strText = strDir + "winver.txt";
                string strRun = strDir + "run.bat";
                File.WriteAllText(strRun, "ver " + " >> " + "\"" + strText + "\"");

                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = strRun;

                psi.UseShellExecute = true;
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                Process prNew = new Process();
                prNew.StartInfo = psi;
                prNew.Start();

                prNew.WaitForExit();

                string version = File.ReadAllText(strText);
                try { File.Delete(strText); }
                catch { }
                try { File.Delete(strRun); }
                catch { }
                Regex regNew = new Regex(@"\[Version *[0-9.]+\]", RegexOptions.IgnoreCase);
                Match mch = regNew.Match(version);
                if (mch.Success)
                {
                    Match innerMatch = Regex.Match(mch.Value, @"[0-9]\.[0-9]", RegexOptions.IgnoreCase);
                    if (innerMatch.Success)
                    {
                        return double.Parse(innerMatch.Value);
                    }
                    else
                    {
                        return 6.1;
                    }

                }
                else
                {
                    return 6.1;
                }
            }
            catch
            {
                return 6.1;
            }






        }
    }
}
